#include "stdafx.h"
#include "RecordDialog.h"
#include "resource.h"
#include "StrCodeUtil.h"
#include "NotifyLibFuncErrorUtil.h"

RecordDialog::RecordDialog(void)
    : m_recFilePath("")
    , m_bIsRecording(false)
#if DD_USING_SA_VOE_WAPPER
    , m_pVoeWarpper(nullptr)
#endif //DD_USING_SA_VOE_WAPPER
{
}


RecordDialog::~RecordDialog(void)
{
}


void RecordDialog::init(HWND hDlg)
{
    m_hDlg = hDlg;

#if DD_USING_SA_VOE_WAPPER
    int iLibRev = 0;
    m_pVoeWarpper = NS_SA::SaVoeWrapper::GetInstance();
    if (!m_pVoeWarpper || (iLibRev = m_pVoeWarpper->Init()) || 0 != iLibRev)
    {
        NotifyLibFuncErrorUtil::NotifyFuncError(m_hDlg, "init", iLibRev);
    }

    // record dev
    {
        int iRecDevNum = m_pVoeWarpper->RecordingDevicesNum();
        if (iRecDevNum <= 0)
        {
            SendDlgItemMessage(m_hDlg, IDC_COMBO_REC_DEV, (UINT)CB_ADDSTRING, (WPARAM)0, (LPARAM)TEXT("None"));
        }
        else
        {
            char bufUtf8Name[1024];
            std::string strUtf8Name("");
            for (int i = 0; i < iRecDevNum; i++)
            {
                memset(bufUtf8Name, 0x00, sizeof(bufUtf8Name));
                m_pVoeWarpper->RecordingDeviceName(i, bufUtf8Name, sizeof(bufUtf8Name));

                // to wstring
                strUtf8Name = bufUtf8Name;
                std::wstring strName = StrCodeUtil::utf8_decode(strUtf8Name);
                SendDlgItemMessage(m_hDlg, IDC_COMBO_REC_DEV, (UINT)CB_ADDSTRING, (WPARAM)0, (LPARAM)strName.c_str());
            }

            // defalut select
            SendDlgItemMessage(m_hDlg, IDC_COMBO_REC_DEV, (UINT)CB_SETCURSEL, (WPARAM)0, (LPARAM)0);
        }
    }

    // record codec
    {
        int iRecCodecNum = m_pVoeWarpper->SupportRecordLocalFileCodecInstNum();
        if (iRecCodecNum <= 0)
        {
            SendDlgItemMessage(m_hDlg, IDC_COMBO_REC_FORMAT, (UINT)CB_ADDSTRING, (WPARAM)0, (LPARAM)TEXT("None"));
        }
        else
        {
            char bufUtf8CodecDisp[1024];
            std::string strUtf8CodecDisp("");
            for (int i = 0; i < iRecCodecNum; i++)
            {
                memset(bufUtf8CodecDisp, 0x00, sizeof(bufUtf8CodecDisp));
                m_pVoeWarpper->SupportRecordLocalFileCodecInstDisp(i, bufUtf8CodecDisp, sizeof(bufUtf8CodecDisp));

                // to wstring
                strUtf8CodecDisp = bufUtf8CodecDisp;
                std::wstring strDisp = StrCodeUtil::utf8_decode(strUtf8CodecDisp);
                SendDlgItemMessage(m_hDlg, IDC_COMBO_REC_FORMAT, (UINT)CB_ADDSTRING, (WPARAM)0, (LPARAM)strDisp.c_str());
            }

            // defalut select
            SendDlgItemMessage(m_hDlg, IDC_COMBO_REC_FORMAT, (UINT)CB_SETCURSEL, (WPARAM)0, (LPARAM)0);
        }
    }

#else
    voe_init("logWebRtcRecord.log", webrtc::kTraceError | webrtc::kTraceDebug | webrtc::kTraceInfo | webrtc::kTraceWarning, webrtc::kTraceModuleAll);
    voe_set_log_path("logWebRtcRecord.log");

    // record dev
    {
        int iRecDevNum = 0;
        voe_get_rec_device_count(iRecDevNum);
        if (iRecDevNum <= 0)
        {
            SendDlgItemMessage(m_hDlg, IDC_COMBO_REC_DEV, (UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) TEXT("None"));
        }
        else
        {
            char strNameUTF8[128];
            char strGuidUTF8[128];
            for (int i = 0; i < iRecDevNum; i++)
            {
                memset(strNameUTF8, 0x00, sizeof(strNameUTF8));
                memset(strGuidUTF8, 0x00, sizeof(strGuidUTF8));
                voe_get_rec_device_name(i, strNameUTF8, strGuidUTF8);

                // to wstring
                std::string strUtf8Name(strNameUTF8);
                std::wstring strName = StrCodeUtil::utf8_decode(strUtf8Name);
                SendDlgItemMessage(m_hDlg, IDC_COMBO_REC_DEV, (UINT)CB_ADDSTRING, (WPARAM)0, (LPARAM)strName.c_str());
            }

            // defalut select
            SendDlgItemMessage(m_hDlg, IDC_COMBO_REC_DEV, (UINT)CB_SETCURSEL, (WPARAM)0, (LPARAM)0);
        }
    }

    // record format
    {
        int iRecFormatNum = voe_get_rec_local_file_format_count();
        if (iRecFormatNum <= 0)
        {
            SendDlgItemMessage(m_hDlg, IDC_COMBO_REC_FORMAT, (UINT)CB_ADDSTRING, (WPARAM)0, (LPARAM)TEXT("None"));
        }
        else
        {
            char strUtf8Disp[1024];
            for (int i = 0; i < iRecFormatNum; i++)
            {
                memset(strUtf8Disp, 0x00, sizeof(strUtf8Disp));
                voe_get_rec_local_file_format_name(i, strUtf8Disp);

                // to wstring
                std::wstring strDisp = StrCodeUtil::utf8_decode(strUtf8Disp);
                SendDlgItemMessage(m_hDlg, IDC_COMBO_REC_FORMAT, (UINT)CB_ADDSTRING, (WPARAM)0, (LPARAM)strDisp.c_str());
            }

            // defalut select
            SendDlgItemMessage(m_hDlg, IDC_COMBO_REC_FORMAT, (UINT)CB_SETCURSEL, (WPARAM)0, (LPARAM)0);
        }
    }
#endif //DD_USING_SA_VOE_WAPPER
}


void RecordDialog::unInit()
{
#if DD_USING_SA_VOE_WAPPER
    NS_SA::SaVoeWrapper::Destory();
    m_pVoeWarpper = nullptr;
    m_bIsRecording = false;
#else
    voe_release();
#endif //DD_USING_SA_VOE_WAPPER
}


void RecordDialog::startRecord()
{
    // get dev channel
    int iDevIdx = SendDlgItemMessage(m_hDlg, IDC_COMBO_REC_DEV, (UINT)CB_GETCURSEL, (WPARAM)0, (LPARAM)0);
    // get codec index
    int iCodecIdx = SendDlgItemMessage(m_hDlg, IDC_COMBO_REC_FORMAT, (UINT)CB_GETCURSEL, (WPARAM)0, (LPARAM)0);

    // get input file name
    int iCharNum = SendDlgItemMessage(m_hDlg, IDC_EDIT_PATH, (UINT)WM_GETTEXTLENGTH, (WPARAM)0, (LPARAM)0);
    iCharNum++;
    TCHAR* pWstrBuf = new TCHAR[iCharNum];
    SendDlgItemMessage(m_hDlg, IDC_EDIT_PATH, (UINT)WM_GETTEXT, (WPARAM)iCharNum, (LPARAM)pWstrBuf);
    std::wstring wstrPath(pWstrBuf);
    std::string strPath = StrCodeUtil::utf8_encode(wstrPath);
    delete pWstrBuf;

#if DD_USING_SA_VOE_WAPPER
    int iLibRev = 0;
    iLibRev = m_pVoeWarpper->SetRecordLocalFileCodecInst(iCodecIdx);
    if (0 != iLibRev)
    {
        NotifyLibFuncErrorUtil::NotifyFuncError(m_hDlg, "SetRecordLocalFileCodecInst", iLibRev);
    }

    iLibRev = m_pVoeWarpper->SetRecordingDevice(iDevIdx);
    if (0 != iLibRev)
    {
        NotifyLibFuncErrorUtil::NotifyFuncError(m_hDlg, "SetRecordingDevice", iLibRev);
    }

    iLibRev = m_pVoeWarpper->StartRecordingMicrophone(strPath.c_str());
    if (0 != iLibRev)
    {
        NotifyLibFuncErrorUtil::NotifyFuncError(m_hDlg, "StartRecordingMicrophone", iLibRev);
    }
#else
    // record format
    if (0 != voe_set_rec_local_file_format(iCodecIdx))
    {
        MessageBox(m_hDlg, TEXT("voe_set_rec_device failed"), TEXT("webrtc error"), MB_OK);
        return;
    }

    // play
    if (0 != voe_set_rec_device(iDevIdx))
    {
        MessageBox(m_hDlg, TEXT("voe_set_rec_device failed"), TEXT("webrtc error"), MB_OK);
        return;
    }

    if (0 != voe_start_record_mircophone(strPath.c_str()))
    {
        MessageBox(m_hDlg, TEXT("voe_start_record_mircophone failed"), TEXT("webrtc error"), MB_OK);
        return;
    }
#endif //DD_USING_SA_VOE_WAPPER

    // record
    m_bIsRecording = true;
}


void RecordDialog::stopRecord()
{
    if (m_bIsRecording)
    {
        #if DD_USING_SA_VOE_WAPPER
        int iLibRev = m_pVoeWarpper->StopRecordingMicrophone();
        if (0 != iLibRev)
        {
            NotifyLibFuncErrorUtil::NotifyFuncError(m_hDlg, "StopRecordingMicrophone", iLibRev);
        }
        #else
        voe_stop_record_mircophone();
        #endif //DD_USING_SA_VOE_WAPPER
    }

    m_bIsRecording = false;
}


bool RecordDialog::isRecording(void) const
{
    return m_bIsRecording;
}